package com.example.ordersystem;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.ordersystem.pojo.Store;
import com.example.ordersystem.utils.InitMthod;
import com.example.ordersystem.utils.ListDataSave;
import com.example.ordersystem.utils.StoreAdapter;

import java.util.ArrayList;
import java.util.List;

/*
 * 首页fragment
 */
public class HomeFragment extends Fragment {
    private List<Store> storeList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.homepage, container, false);

        InitMthod.getStoreList(getContext());// 初始化店铺信息
        InitMthod.getMenuList(getContext());//初始化菜单信息

        storeList = ListDataSave.getStoreList(getContext());

        StoreAdapter adapter = new StoreAdapter(getActivity(), R.layout.store, this.storeList);
        ListView listView = view.findViewById(R.id.list_view);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Store store = HomeFragment.this.storeList.get(position);
                //传递参数给菜单的activity  店铺名或者店铺id
                Intent intent = new Intent(getActivity(), MenuActivity.class);
                intent.putExtra("storeName", store.getStoreName());//传递参数
                startActivity(intent);
            }
        });


        return view;
    }
}
